package com.itany.nmms.service.proxy;

import com.github.pagehelper.PageInfo;
import com.itany.nmms.entity.Dept;
import com.itany.nmms.entity.Staff;
import com.itany.nmms.exception.*;
import com.itany.nmms.factory.ObjectFactory;
import com.itany.nmms.service.DeptService;
import com.itany.nmms.tran.TransactionManager;

import java.util.List;

/**
 * @author 石小俊
 * @date 2024年05月15日 18:41
 */
public class DeptServiceProxy implements DeptService {
    @Override
    public void addFatherDept(String name, String remark, Staff staff) throws LoginDisabledException, RequestParameterErrorException, DeptExistException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        DeptService deptService = (DeptService) ObjectFactory.getObject("deptServiceTarget");
        try {
            tran.begin();
            deptService.addFatherDept(name, remark, staff);
            tran.commit();
        } catch (LoginDisabledException e) {
            tran.rollback();
            throw e;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (DeptExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void addSonDept(String fatherId, String name, String remark, Staff staff) throws LoginDisabledException, RequestParameterErrorException, DeptExistException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        DeptService deptService = (DeptService) ObjectFactory.getObject("deptServiceTarget");
        try {
            tran.begin();
            deptService.addSonDept(fatherId, name, remark, staff);
            tran.commit();
        } catch (LoginDisabledException e) {
            tran.rollback();
            throw e;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (DeptExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public PageInfo<Dept> findPage(String pageNo, String pageSize) throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        DeptService deptService = (DeptService) ObjectFactory.getObject("deptServiceTarget");
        try {
            tran.begin();
            PageInfo<Dept> deptPage = deptService.findPage(pageNo, pageSize);
            tran.commit();
            return deptPage;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public Dept findById(String id) throws RequestParameterErrorException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        DeptService deptService = (DeptService) ObjectFactory.getObject("deptServiceTarget");
        try {
            tran.begin();
            Dept dept = deptService.findById(id);
            tran.commit();
            return dept;
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void modifyDept(String id, String name, String remark) throws RequestParameterErrorException, DeptExistException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        DeptService deptService = (DeptService) ObjectFactory.getObject("deptServiceTarget");
        try {
            tran.begin();
            deptService.modifyDept(id, name, remark);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (DeptExistException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void enableDept(String id) throws RequestParameterErrorException, DeptStatusException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        DeptService deptService = (DeptService) ObjectFactory.getObject("deptServiceTarget");
        try {
            tran.begin();
            deptService.enableDept(id);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (DeptStatusException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public void disableDept(String id) throws RequestParameterErrorException, ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        DeptService deptService = (DeptService) ObjectFactory.getObject("deptServiceTarget");
        try {
            tran.begin();
            deptService.disableDept(id);
            tran.commit();
        } catch (RequestParameterErrorException e) {
            tran.rollback();
            throw e;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }

    @Override
    public List<Dept> findAll() throws ServiceException {
        TransactionManager tran = (TransactionManager) ObjectFactory.getObject("tran");
        DeptService deptService = (DeptService) ObjectFactory.getObject("deptServiceTarget");
        try {
            tran.begin();
            List<Dept> depts = deptService.findAll();
            tran.commit();
            return depts;
        } catch (ServiceException e) {
            tran.rollback();
            throw e;
        }
    }
}
